<?php


namespace app\common\classes;


use think\facade\Env;

/**
 * 消费凭证
 * Class VoucherRedis
 * @package app\common\classes
 */
class VoucherRedis
{
    protected $redis;
    protected $host;
    protected $port;
    protected $auth;

    public function __construct()
    {
        $this->host = Env::get('redis.host', 'r-bp11v3wc1977f8zhaj.redis.rds.aliyuncs.com');
        $this->port = 6379;
        $this->auth = Env::get('redis.auth', 'r-bp11v3wc1977f8zhaj:dsfweew%$weew2');
    }

    /**
     * 获取reids对象
     * @return \Redis
     */
    public function getRedis()
    {
        if (!is_object($this->redis)) {
            $this->redis = new \Redis();
            $this->redis->connect($this->host, $this->port);
            $this->redis->auth($this->auth);
        }
        return $this->redis;
    }

    /**
     * 查看此章节用户是否阅读并收费
     * @param $userId
     * @param $novelId
     * @param $chapterNo
     * @return bool
     */
    public function haveReadVoucher($userId, $novelId, $chapterNo)
    {
        return $this->getRedis()->sIsMember("read_voucher_{$novelId}_{$chapterNo}", $userId);
    }

    /**
     * 插入消费凭证
     * @param $userId
     * @param $novelId
     * @param $chapterNo
     * @return bool|int
     */
    public function insertReadVoucher($userId, $novelId, $chapterNo)
    {
        return $this->getRedis()->sAdd("read_voucher_{$novelId}_{$chapterNo}", $userId);
    }

    /**
     * 删除消费凭证
     */
    public function delReadVoucher(){

        echo "开始".PHP_EOL;
        $iterator = null;
        $pattern = 'read_voucher_*';
        do {
            $keys = $this->getRedis()->scan($iterator, $pattern);
            if ($keys !== false) {
                // 删除匹配的键
                foreach ($keys as $key) {
                    echo $key.PHP_EOL;
                    $r = $this->getRedis()->del($key);
                    var_dump($r);
                }
            }
        } while ($iterator > 0);



        echo "Deleted keys starting with 'read_voucher_' successfully.".PHP_EOL;

        $this->getRedis()->close();
    }

    /**
     * 记录小说每日uv
     * @param $qid
     * @param $bookId
     * @param $date
     * @param $userId
     */
    function addBookUv($qid, $bookId, $date, $userId)
    {
        //分快应用
        $Key = "book_uv_{$qid}_{$date}:" . $bookId;
        $this->getRedis()->sAdd($Key, $userId);
        $timeout = 259200;
        $this->getRedis()->expire($Key, $timeout);

        //记总的
        $Key2 = "book_uv_all_{$date}:" . $bookId;
        $this->getRedis()->sAdd($Key2, $userId);
        $timeout = 259200;
        $this->getRedis()->expire($Key2, $timeout);
    }

    /**
     * 获取小说每日uv（分快应用）
     * @param $qid
     * @param $bookId
     * @param $date
     * @return int
     */
    function getBookUvCount($qid, $bookId, $date)
    {
        $Key = "book_uv_{$qid}_{$date}:" . $bookId;
        return $this->getRedis()->sCard($Key);
    }

    /**
     * 获取小说每日uv（总的）
     * @param $bookId
     * @param $date
     * @return int
     */
    function getAllBookUvCount($bookId, $date)
    {
        $Key = "book_uv_all_{$date}:" . $bookId;
        return $this->getRedis()->sCard($Key);
    }
}